package com.example.yunhailutong.zhongtan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.util.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityRegister extends ActivityBase {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.phone_register)
    EditText phoneRegister;
    @BindView(R.id.id_card)
    EditText idCardNo;
    @BindView(R.id.password1_register)
    EditText password1Register;
    @BindView(R.id.password2_register)
    EditText password2Register;
    @BindView(R.id.update_register)
    Button updateRegister;
    private String idcardno;
    @Override
    protected void addListener() {
        updateRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idcardno = idCardNo.getText().toString();
                if (!Utils.isMobileNO(phoneRegister.getText().toString())) {
                    disPlay("请输入有效的手机号");
                } else if (password1Register.getText().toString().equals("") || password2Register.getText().toString().equals("")) {
                    disPlay("密码不能为空请重新输入");
                } else if (!password1Register.getText().toString().equals(password2Register.getText().toString())) {
                    disPlay("两次密码输入不一致请核对后再次提交");
                } else if (idcardno.isEmpty() || (idcardno.length() != 15 && idcardno.length() != 18)) {
                    disPlay("请输入有效的身份证号");
                } else {
                    Map<String, String> params = new HashMap<>();
                    params.put("idCard", idCardNo.getText().toString());
                    params.put("phone", phoneRegister.getText().toString());
                    params.put("pwd", password1Register.getText().toString());
                    register(params);
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_register;
    }

    public void register(Map<String, String> params) {
        new PersionBiz().register(params);
        showWaitingDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void log(OldsEvent.RegisterEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            disPlay("注册成功！");
            finish();
        }
    }
}
